package com.sp.scmdb.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.MapBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.entity.Map;
import com.sp.scmdb.service.MapService;

@Controller("mapAction")
@Scope("prototype")
public class MapAction extends BaseAction<MapBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(MapAction.class);
	
	private MapService mapService;
	private InputStream imagStream;

	@SuppressWarnings("unchecked")
	public String findAll() {
		try{
			List<Map> mapList = mapService.queryMap();
			entity.setAllList(mapList);
			entity.setList(mapList.subList(entity.getCurrentRow(), entity.getLastRow()));
			session.put("useType", CommonParamter.TypeCommon.getMapUseType());
			this.button("Super.Popup(5,'添加地图','/SCMDB/mapAction!newMap.action','',600,370)", 
					"Super.editData('/SCMDB/mapAction!findMap.action?id=',600,370)", 
					"Super.delData('/SCMDB/mapAction!delMap.action?id=')");
		}catch(Exception e){
			return "login";
		}
		return SUCCESS;
	}
	public String newMap(){
		return "newSucc";
	}
	public String addMap() {
		try{
			UserBean user = (UserBean) session.get("user");
			String result = mapService.addMap(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result,"地图");
		}catch(Exception e){
			e.printStackTrace();
			log.error("add fail");
			return "fail";
		}
		return "commitSucc";
	}

	public String findMap() {
		try{
			String mapId = request.getParameter("id");
			List<Map> list = mapService.findMap(mapId);
			if(CollectionUtils.isEmpty(list)){
				request.setAttribute("message","地图不存在！");
			}
			else{
				request.setAttribute("mapList", list);
			}
		}catch(Exception e){
			log.error("add fail");
			return "login";
		}
		return "editSucc";
	}
	
	public String editMap() {
		try{
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			String result = mapService.editMap(entity, depId, user);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result,"地图");
		}catch(Exception e){
			e.printStackTrace();
			log.error("edit fail");
			return "fail";
		}
		return "commitSucc";
	}
	
	public String getMap(){
		String id = request.getParameter("id");
		Map map = mapService.getMapById(id);
		if (map == null) {
			request.setAttribute("message", "地图不存在！");
		} else {			
			imagStream = new ByteArrayInputStream(map.getMapAddr());
		}
		return "mapImag";
	}
	
	public String delMap() {
		try{
			UserBean user = (UserBean) session.get("user");
			String mapId = request.getParameter("id");
			if(StringUtils.isNotEmpty(mapId)){
				String []arr = mapId.split(",");
				String result = mapService.delMap(arr, user);
				if (log.isInfoEnabled()) {
					log.info("delete suncce");
				}
				messageSession(result,"地图");
			}
		}catch(Exception e){
			log.error("delete fail");
			return "fail";
		}
		return "commitSucc";
	}
	
	public InputStream getImagStream() {
		return imagStream;
	}

	public void setImagStream(InputStream imagStream) {
		this.imagStream = imagStream;
	}

	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}
}
